package com.pic.mgt.haishin.action;

import java.util.List;

import org.apache.commons.lang.StringUtils;

import com.pic.mgt.haishin.manager.business.HaishinBusinessManager;
import com.pic.mgt.manager.business.BasicBusinessInfoBean;
import com.pic.mgt.manager.business.BusinessManagementOperationException;

public class BusinessListMgtActionImpl implements BusinessListMgtAction {
	
	private HaishinBusinessManager haishinBusinessManager;

	@Override
	public boolean addBusiness(String businessName, String businessType) {
		try {
			this.haishinBusinessManager.addBusiness(businessName, businessType);
			
			return true;
		} catch (BusinessManagementOperationException e) {
			// 不用寫 Log , 已寫
			return false;
		}
	}

	@Override
	public String getEffectiveBusinessListInfoString() {
		List<BasicBusinessInfoBean> basicBusinessInfoBeanList =
			this.haishinBusinessManager.getEffectiveBasicBusinessInfoBeanList();
		
		return
			formatBusiness(basicBusinessInfoBeanList);
	}

	@Override
	public boolean removeBusiness(String businessName) {
		try {
			this.haishinBusinessManager.removeBusiness(businessName);
			
			return true;
		} catch (BusinessManagementOperationException e) {
			// 不用寫 Log , 已寫
			return false;
		}
	}
	
	
	/**
	 * 將 Business 的資訊格式化後回傳
	 * @param basicBusinessInfoBeanList
	 * @return
	 */
	public static String formatBusiness(List<BasicBusinessInfoBean> basicBusinessInfoBeanList) {
		StringBuffer businessListStringBuf = new StringBuffer();
		businessListStringBuf.append(StringUtils.rightPad("Business Name", 15));
		businessListStringBuf.append("Business Type");
		businessListStringBuf.append("\r\n");
		businessListStringBuf.append("------------------------------\r\n");
		
		for (BasicBusinessInfoBean basicBusinessInfoBean : basicBusinessInfoBeanList) {
			businessListStringBuf.append(StringUtils.rightPad(basicBusinessInfoBean.getBusinessName(),15));
			businessListStringBuf.append(basicBusinessInfoBean.getBusinessType());
			businessListStringBuf.append("\r\n");
		}
		
		return businessListStringBuf.toString();
	}
	
	// --- getter and setter ---
	
	public void setHaishinBusinessManager(
			HaishinBusinessManager haishinBusinessManager) {
		this.haishinBusinessManager = haishinBusinessManager;
	}
}
